/*
 * Copyright (c) 2023, gaoweixuan (breeze-cloud@foxmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.breeze.boot.satoken.endpoint.sso;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.exception.SaSsoException;
import cn.dev33.satoken.sso.message.SaSsoMessage;
import cn.dev33.satoken.sso.processor.SaSsoClientProcessor;
import cn.dev33.satoken.sso.template.SaSsoClientUtil;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.breeze.boot.core.constants.CoreConstants.X_TENANT_ID;

/**
 * Sa-Token-SSO Server端 Controller
 *
 * @author gaoweixuan
 * @since 2024/09/11
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class SsoClientEndPoint {

    /**
     * 处理 SSO-Client 端所有请求
     *
     * @return {@link Object }
     * <p>
     * SSO-Client端：处理所有SSO相关请求
     * http://{host}:{port}/sso/login			-- Client端登录地址，接受参数：back=登录后的跳转地址
     * http://{host}:{port}/sso/logout			-- Client端单点注销地址（isSlo=true时打开），接受参数：back=注销后的跳转地址
     * http://{host}:{port}/sso/logoutCall		-- Client端单点注销回调地址（isSlo=true时打开），此接口为框架回调，开发者无需关心
     */
    @RequestMapping({"/sso/login", "/sso/pushC", "/sso/logout", "/sso/logoutCall"})
    public Object ssoClientRequest() {
        log.info("---------------- sso client 请求地址：{}", SaHolder.getRequest().getRequestPath());
        return SaSsoClientProcessor.instance.dister();
    }

    /**
     * 当前应用独自注销 (不退出其它应用)
     */
    @RequestMapping("/sso/logoutByAlone")
    public Object logoutByAlone() {
        StpUtil.logout();
        return SaSsoClientProcessor.instance._ssoLogoutBack(SaHolder.getRequest(), SaHolder.getResponse());
    }

    /**
     * index
     *
     * @return {@link Object }
     */
    @RequestMapping("/")
    public Object index() {
        return "仅支持前后端分离模式";
    }

    /**
     * 查询我的账号信息：sso-client 前端 -> sso-center 后端 -> sso-server 后端
     *
     * @return {@link Object }
     */
    @RequestMapping("/sso/userInfo")
    public Object userInfo(@RequestHeader(X_TENANT_ID) String XTenantId) {
        // 如果尚未登录
        if (!StpUtil.isLogin()) {
            throw new SaSsoException("尚未登录");
        }
        // 推送消息
        SaSsoMessage message = new SaSsoMessage();
        message.setType("userInfo");
        message.set("loginId", StpUtil.getLoginId());
        message.set("client", SaSsoManager.getClientConfig().getClient());
        message.set(X_TENANT_ID, XTenantId);
        return SaSsoClientUtil.pushMessageAsSaResult(message);
    }
}
